package com.hd.crdao.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hd.crdao.R;
import com.hd.crdao.bean.LoanCarListBean;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.StringUtils;
import com.hd.crdao.utils.UIUtils;

import java.util.List;

/**
 *
 *@author Programming
 *@date 2017/6/5 0005
 */


public class LoanCarAdapter extends RecyclerView.Adapter {

    private List<LoanCarListBean.RowsBean> mDataSets;

    private OnItemClickListener mOnItemClickListener;

    public LoanCarAdapter(List<LoanCarListBean.RowsBean> dataSets) {
        this.mDataSets = dataSets;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.loancar_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        LoanCarListBean.RowsBean rowsBean = mDataSets.get(position);
        viewHolder.mLoancarName.setText(rowsBean.getVeBrand());
        if (rowsBean.getProgressDate() != null) {
            viewHolder.mLoancarTime.setText(StringUtils.getDate(rowsBean.getProgressDate()));
        } else {
            viewHolder.mLoancarTime.setText("");
        }
        viewHolder.mLoancarNumber.setText(rowsBean.getVeFrame());
        int voStatus = rowsBean.getVoStatus();
        viewHolder.mLoancarvcCredit.setText(StringUtils.doubleToString(rowsBean.getVehCredit().getVcCredit()));
        viewHolder.mLoancarvcRate.setText(String.valueOf(rowsBean.getVehCredit().getVcRate()) + "%");
        int checkStatus = rowsBean.getVoStatus();
        if (voStatus == 2) {
            if (checkStatus == 0) {
                //  viewHolder.mCheckStatus.setBackgroundResource(R.drawable.yipandian_2x);
                viewHolder.mItemLoancarHead.setImageResource(R.drawable.weipan_2x);
            } else if (checkStatus == 1) {
                viewHolder.mItemLoancarHead.setImageResource(R.drawable.yipand_2x);
            }
        }
        //设置贷款车辆的状态
        if (voStatus == 0) {
            viewHolder.mLoancarState.setText("初始");
        } else if (voStatus == 1) {
            viewHolder.mLoancarState.setText("已提档");
        } else if (voStatus == 2) {
            viewHolder.mLoancarState.setText("已贷款");
        } else if (voStatus == 3) {
            viewHolder.mLoancarState.setText("已售出");
        } else {
            viewHolder.mLoancarState.setText("已清偿");
        }
        String string = SPUtils.getString("childuser", "");
        if (!TextUtils.isEmpty(string)) {
            viewHolder.mChildSetLayout.setVisibility(View.GONE);
        } else {
            viewHolder.mChildSetLayout.setVisibility(View.VISIBLE);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, viewHolder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mDataSets != null) {
            return mDataSets.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mItemLoancarHead;
        TextView mLoancarName;
        TextView mLoancarTime;
        TextView mLoancarNumber;
        TextView mLoancarState;
        TextView mLoancarvcCredit;
        TextView mLoancarvcRate;
        //  ImageView mCheckStatus;
        LinearLayout mChildSetLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            mItemLoancarHead = (ImageView) itemView.findViewById(R.id.item_loancar_head);
            mLoancarName = (TextView) itemView.findViewById(R.id.loancar_name);
            mLoancarNumber = (TextView) itemView.findViewById(R.id.loancar_number);
            mLoancarState = (TextView) itemView.findViewById(R.id.loancar_state);
            mLoancarTime = (TextView) itemView.findViewById(R.id.loancar_time);
            mLoancarvcCredit = (TextView) itemView.findViewById(R.id.loancar_vcCredit);
            mLoancarvcRate = (TextView) itemView.findViewById(R.id.loancar_vcRate);
            mChildSetLayout = (LinearLayout) itemView.findViewById(R.id.childset_layout);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}